from fastapi import Request
from app.api.admin.config import AdminConfig
from app.exceptions.exception import AppException
from app.exceptions.http_resp import HttpResp
from app.modules.auth.manager import token_manager


async def admin_verify_token(request: Request):
    # 路由转权限
    auths = request.url.path.replace("/api/", "").replace('/', ':')
    # print("admin_verify_token", auths)
    # 免登录
    if auths in AdminConfig.not_login_uri:
        return

    # token是否为空
    token = request.headers.get('Authorization', '')
    # print("token", token)
    if not token:
        raise AppException(HttpResp.TOKEN_EMPTY)

    # token 是否正确
    user_id = token_manager.get_identity(token)
    # print("payload", token_manager._parse_payload(token))

    # token 是否过期
    # 校验用户被删除
    # 单次请求信息保存
    request.state.user_id = user_id
    # 免权限验证
    # 检验校色是否存在
    # 校验用户是否有权限


async def h5_verify_token(request: Request):
    print("h5_verify_token is run")
    # token是否为空
    token = request.headers.get('Authorization', '')
    print("token", token)
    if not token:
        raise AppException(HttpResp.TOKEN_EMPTY)

    # token 是否正确
    client_id = token_manager.get_identity(token)
    print("payload", client_id)

    # token 是否过期
    # 校验用户被删除
    # 单次请求信息保存
    request.state.client_id = client_id
